package com.meteor.web.controller;

import com.meteor.pojo.Comment;
import com.meteor.service.CommentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {

    @Resource
    private CommentService commentService;

    @Value("/image/img.png")
    private String avatar;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model) {
        List<Comment> comments = commentService.queryCommentByBlogId(blogId);
        model.addAttribute("comments",comments);
        return "blog::commentList";
    }

    @PostMapping("/comments")
    public String post(Comment comment,Model model) {
        Date currentTime = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format = sdf.format(currentTime);
        comment.setCreateTime(format);
        comment.setAvatar(avatar);
        comment.setAdminComment(true);
        int i = commentService.insertComment(comment);
        return "redirect:/comments/" + comment.getBlogId();
    }
}
